const OSS = require('ali-oss');
const path = require('path');
var fs = require('fs')

const client = new OSS({
    region: 'oss-cn-beijing',
    accessKeyId: 'LTAIcGfolFkSDbtG',
    accessKeySecret: 'YtScXrqfX6HUUYYFTYDFqepVtuzwXa',
    bucket: 'stock-cn'
});

// var html = fs.readFileSync('./build/web-mobile/index.html', 'utf8')
// html = html.replace("Cocos Creator | nbadatareport","兔年年终报告")
// html = html.replace(`<link rel="icon" href="favicon.8de18.ico"/>`, "")
// fs.writeFileSync('./build/web-mobile/index.html', html, 'utf8')
// fs.copyFileSync('splash.85cfd.png', 'build/web-mobile/splash.85cfd.png')
const uploadDir = async (dir, targetDir) => {
    const files = fs.readdirSync(dir);

    for (let file of files) {
        const filePath = path.join(dir, file);
        const stat = fs.statSync(filePath);

        if (stat.isFile()) {
            // 上传文件到OSS
            const result = await client.put(targetDir + '/' + file, filePath);
            console.log(`File uploaded: ${result.url}`);
        } else if (stat.isDirectory()) {
            // 如果是目录，则递归上传
            await uploadDir(filePath, targetDir + '/' + file);
        }
    }
};

(async()=>{
    // 上传到test文件夹
    await uploadDir('build/web-mobile', '/report/test');

    // 上传到prod文件夹
    await uploadDir('build/web-mobile', '/report/prod');
})();
